//
//  ChongzhiViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/14.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "ChongzhiViewController.h"
#import "JTCSM.h"
#import "DaiKuanTableViewCell.h"
#import "GetInfoClass.h"
#import "UserInfoModel.h"
#import "ChongzhiResultViewController.h"
#import "ZhifuView.h"
static CGFloat btnspace = 40;
static CGFloat btnHeight = 50;

@interface ChongzhiViewController ()<UITableViewDelegate,UITableViewDataSource,zhifuViewDelegate>
{
    NSArray *titlearray;
    UITableView *tableview;
    UserInfoModel *myinfoModel;
    NSString *total_price;
    NSString *type;
    NSString *client_num;
}
@end

@implementation ChongzhiViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[GetInfoClass shareSingle] getinfo];
    [GetInfoClass shareSingle].myblock = ^(UserInfoModel *Model) {
        myinfoModel = Model;
        [tableview reloadData];
        
    };

}

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    [df isRemoveTarbar:YES];
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    // Do any additional setup after loading the view.
    total_price = @"";
    type = @"";
    client_num = @"";
    titlearray =@[@[@"账户余额",@"充值金额"], @[@"微信支付",@"支付宝支付"],@[@"确       认"]];
    [self layoutTableView];
}

- (void)layoutTableView{
    
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KCONTROLLERHeight-50) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //    view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return titlearray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *contentArray = titlearray[section];
    return contentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NSString *cellid = [NSString stringWithFormat:@"textfield%ld",(long)indexPath.row];
        DaiKuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[DaiKuanTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid TableViewCellType:DaiKuanTableViewCellTextField];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = titlearray[indexPath.section][indexPath.row];
        if (indexPath.row==0) {
            cell.textfield.text = [NSString stringWithFormat:@"%.2f",[myinfoModel.money floatValue]];
            cell.textfield.enabled = NO;
        }else{
            [cell.textfield addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];

        }
        return cell;
    }
    if (indexPath.section == 1) {
        NSString *cellid = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        DaiKuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[DaiKuanTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid TableViewCellType:DaiKuanTableViewCellImageView];
        }
        [ZYRadioButton addObserverForGroupId:KImageRadio observer:self];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = titlearray[indexPath.section][indexPath.row];
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_0%ld@2x",indexPath.row+5]];
        return cell;
    }

    if (indexPath.section == 2) {
        NSString *cellid = [NSString stringWithFormat:@"button%ld",(long)indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
        }
        UIButton *LoginBtn =[[UIButton alloc] initWithFrame:CGRectMake(btnspace, 20 , (KCONTROLLERWith-2*btnspace), btnHeight)];
        LoginBtn.backgroundColor =RGBACOLOR(227, 68, 40, 1);
        [LoginBtn setTitle:titlearray[indexPath.section][indexPath.row] forState:UIControlStateNormal];
        [LoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [LoginBtn addTarget:self action:@selector(gotoMainV) forControlEvents:UIControlEventTouchUpInside];
        LoginBtn.tag = 10002;
        [cell.contentView addSubview:LoginBtn];
        [df layerWithView:LoginBtn radius:btnHeight/2 width:0 color:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        return btnspace+btnHeight;
    }
    return [DaiKuanTableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableView];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UILabel initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, section==1?40:0) text:@"       选择支付方式" font:16 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(89, 89, 89, 1) backgroundColor:RGBACOLOR(241, 241, 241, 1)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section==1?40:0;
}

-(void)gotoMainV{
        //    Bonus/receive
 LoadData *request = [[LoadData alloc]init];
 [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"UserRecharge/create") withDict:@{@"token":KTokenString,@"total_price":total_price,@"type":type} superView:nil isAddtag:YES Block:^(id result) {
     
     NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
     
          if ([isreturn isEqualToString:@"1"]) {
//          "qrcode":"jpg","poster":"jpg","total_price":100,"client_num":"xx"
              NSDictionary *dict = KResultOfKey(@"data");
              [ZhifuView shareSingle].delegate = self;
              [[ZhifuView shareSingle] setMoney:[dict objectForKey:@"total_price"] imagename:[dict objectForKey:@"qrcode"] saveIamge:[dict objectForKey:@"poster"]];
              client_num = [dict objectForKey:@"client_num"];
          
          }
 }];
        
}

#pragma mark 支付结果

- (void)zhifuResultWithType:(zhifuresultType)zhifutype{
    
    if (zhifutype == Success) {
        LoadData *request = [[LoadData alloc]init];
        [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"UserRecharge/checkOk") withDict:@{@"token":KTokenString,@"client_num":client_num} superView:nil isAddtag:YES Block:^(id result) {
            
            NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
            
            
                 if ([isreturn isEqualToString:@"1"]) {
                     ChongzhiResultViewController *vc = [[ChongzhiResultViewController alloc]init];
                     vc.nameTitle = @"充值结果";
                     vc.titleString = [NSString stringWithFormat:@"+%@元",total_price];
                     vc.isSuccess = YES;
                     [self.navigationController pushViewController:vc animated:YES];
                 }else{
                     ChongzhiResultViewController *vc = [[ChongzhiResultViewController alloc]init];
                     vc.nameTitle = @"充值结果";
                     vc.titleString = [NSString stringWithFormat:@"+%@元",total_price];
                     vc.isSuccess = NO;
                     [self.navigationController pushViewController:vc animated:YES];
            
                 }
            [ZhifuView shareSingle].hidden = YES;
            
            
        }];

    }
    
    if (zhifutype == Fail) {
        [ZhifuView shareSingle].hidden = YES;
    }
}


#pragma mark 监听textfield 的变化
- (void)valueChanged:(UITextField *)textfield{
    
    total_price = textfield.text;
}

-(void)radioButtonSelectedAtIndex:(NSUInteger)index inGroup:(NSString*)groupId{
    
    if (index == 0) {
        //微信
        NSLog(@"微信");
        type = @"1";
    }
    if (index == 1) {
        
        //支付宝
        NSLog(@"支付宝");
        type = @"2";
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
